package com.yanqu.road.dao.impl.union.cross;

import com.alibaba.fastjson.JSONArray;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.trade.UnionTradeEventRead;
import com.yanqu.road.entity.union.trade.UnionTradeLog;
import com.yanqu.road.entity.union.trade.UnionTradeTeamData;
import com.yanqu.road.entity.union.trade.UnionTradeTeamUserJoin;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionTradeTeamDaoImpl extends TempDataObjectDao<UnionTradeTeamData> {

    @Override
    protected boolean addData(UnionTradeTeamData data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_union_trade_team(unionUid,routeId,openUser,openTime,startTradeTime,stateVal,finishType,sendReward,expectReward,reward,eventReward," +
                "logDataList,userJoinDataList,userReadList,eventTimeList,eventList,takeEventIndex,openNum,restTime,userInfoList,`condition`,newRewards) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getUnionUid());
            call.setInt(i++, data.getRouteId());
            call.setLong(i++, data.getOpenUser());
            call.setLong(i++, data.getOpenTime());
            call.setLong(i++, data.getStartTradeTime());
            call.setInt(i++, data.getStateVal());
            call.setInt(i++, data.getFinishType());
            call.setBoolean(i++, data.isSendReward());
            call.setString(i++, data.getExpectReward());
            call.setString(i++, data.getReward());
            call.setString(i++, data.getEventReward());
            call.setString(i++, JSONArray.toJSONString(data.getNewLogDataList()));
            call.setString(i++, JSONArray.toJSONString(data.getNewUserJoinDataList()));
            call.setString(i++, JSONArray.toJSONString(data.getNewUserReadList()));
            call.setString(i++, StringUtils.listToString(data.getEventTimeList(), ";"));
            call.setString(i++, StringUtils.listToString(data.getEventList(), ";"));
            call.setInt(i++, data.getTakeEventIndex());
            call.setInt(i++, data.getOpenNum());
            call.setLong(i++, data.getRestTime());
            call.setString(i++, JSONArray.toJSONString(data.getNewUserInfoList()));
            call.setLong(i++, data.getCondition());
            call.setString(i++, data.getNewRewards());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionTradeTeamData data) {
        PreparedStatement call = null;
        String sql = "update t_c_union_trade_team set openUser=?,openTime=?,startTradeTime=?,stateVal=?,finishType=?,sendReward=?,expectReward=?,reward=?,eventReward=?," +
                "logDataList=?,userJoinDataList=?,userReadList=?,eventTimeList=?,eventList=?,takeEventIndex=?,openNum=?,restTime=?,userInfoList=?,`condition`=?,newRewards=? where unionUid=? and routeId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getOpenUser());
            call.setLong(i++, data.getOpenTime());
            call.setLong(i++, data.getStartTradeTime());
            call.setInt(i++, data.getStateVal());
            call.setInt(i++, data.getFinishType());
            call.setBoolean(i++, data.isSendReward());
            call.setString(i++, data.getExpectReward());
            call.setString(i++, data.getReward());
            call.setString(i++, data.getEventReward());
            call.setString(i++, JSONArray.toJSONString(data.getNewLogDataList()));
            call.setString(i++, JSONArray.toJSONString(data.getNewUserJoinDataList()));
            call.setString(i++, JSONArray.toJSONString(data.getNewUserReadList()));
            call.setString(i++, StringUtils.listToString(data.getEventTimeList(), ";"));
            call.setString(i++, StringUtils.listToString(data.getEventList(), ";"));
            call.setInt(i++, data.getTakeEventIndex());
            call.setInt(i++, data.getOpenNum());
            call.setLong(i++, data.getRestTime());
            call.setString(i++, JSONArray.toJSONString(data.getNewUserInfoList()));
            call.setLong(i++, data.getCondition());
            call.setString(i++, data.getNewRewards());
            call.setString(i++, data.getUnionUid());
            call.setInt(i++, data.getRouteId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UnionTradeTeamData parseData(ResultSet rs) throws SQLException {
        UnionTradeTeamData data = new UnionTradeTeamData();
        data.setUnionUid(rs.getString("unionUid"));
        data.setRouteId(rs.getInt("routeId"));
        data.setOpenUser(rs.getLong("openUser"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setStartTradeTime(rs.getLong("startTradeTime"));
        data.setStateVal(rs.getInt("stateVal"));
        data.setFinishType(rs.getInt("finishType"));
        data.setSendReward(rs.getBoolean("sendReward"));
        data.setExpectReward(rs.getString("expectReward"));
        data.setReward(rs.getString("reward"));
        data.setEventReward(rs.getString("eventReward"));
        data.setLogDataList(JSONArray.parseArray(rs.getString("logDataList"), UnionTradeLog.class));
        data.setUserJoinDataList(JSONArray.parseArray(rs.getString("userJoinDataList"), UnionTradeTeamUserJoin.class));
        data.setUserReadList(JSONArray.parseArray(rs.getString("userReadList"), UnionTradeEventRead.class));
        data.setEventTimeList(StringUtils.stringToIntegerList(rs.getString("eventTimeList"), ";"));
        data.setEventList(StringUtils.stringToIntegerList(rs.getString("eventList"), ";"));
        data.setTakeEventIndex(rs.getInt("takeEventIndex"));
        data.setOpenNum(rs.getInt("openNum"));
        data.setRestTime(rs.getLong("restTime"));
        String userInfoList = rs.getString("userInfoList");
        if(!StringUtils.isNullOrEmpty(userInfoList)) {
            data.setUserInfoList(JSONArray.parseArray(userInfoList, UserBaseInfo.class));
        }
        data.setCondition(rs.getLong("condition"));
        data.setNewRewards(rs.getString("newRewards"));
        return data;
    }

    public Map<String, Map<Integer, UnionTradeTeamData>> getUnionTradeTeamDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Integer, UnionTradeTeamData>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_union_trade_team where finishType <> 3";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionTradeTeamData data = parse(rs);
                Map<Integer, UnionTradeTeamData> innerMap = dataMap.get(data.getUnionUid());
                if(innerMap == null){
                    innerMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getUnionUid(), innerMap);
                }
                innerMap.put(data.getRouteId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
